<template>
  <view>
    <!-- 搜索组件 -->
    <!-- <search :bgcolor="'green'" :radius="10"></search> -->
    <search @click="goSearch"></search>
    <view class="scroll-view-container">
      <scroll-view class="left-scroll-view" :style="{height: wh +'px'}" scroll-y="true">
        <block v-for="(item, i) in cateList" :key="i">
          <view :class="['left-scroll-view-item',i === active ? 'active' : '']" @click="activeChanged(i)">
            {{item.cat_name}}</view>
        </block>
      </scroll-view>

      <scroll-view class="right-scroll-view" :style="{height: wh +'px'}" scroll-y="true" :scroll-top="scrollTop">
        <view class="cate-lv2" v-for="(lv2, index) in cateLevel2" :key="index">
          <view class="cate-lv2-title">/ {{lv2.cat_name}} /</view>
          <!-- 三级分类渲染 -->
          <view class="cate-lv3-list">
            <view class="cate-lv3-item" v-for="(lv3, v3i) in lv2.children" :key="v3i" @click="gotoGoodsList(lv3)">
              <img :src="lv3.cat_icon">
              <text>{{lv3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js' // 导入自己封装的 mixin 模块
  export default {
    mixins: [badgeMix], // 将 badgeMix 混入到当前的页面中进行使用
    data() {
      return {
        active: 0,
        wh: 0, //窗口可用高度
        cateList: [], // 分类数据列表
        cateLevel2: [], // 二级分类列表
        scrollTop: 0 //切换一级分类时重置右侧二三级分类滚动条的位置
      };
    },
    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      console.log(sysInfo.windowHeight)
      // 减去搜索框高度
      this.wh = sysInfo.windowHeight - 50 
      console.log(this.wh)

      // 调用获取分类列表数据的方法
      this.getCateList()
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        // 为默认二级分类赋值
        this.cateLevel2 = res.message[0].children
      },
      activeChanged(i) {
        this.active = i
        this.cateLevel2 = this.cateList[i].children // 为二级分类列表重新赋值
        this.scrollTop = this.scrollTop === 0 ? 1 : 0 // scrollTop必须每次不一样才生效
        // this.scrollTop = this.scrollTop? 1 : 0 // 该方式未生效
      },
      // 从三级分类跳转到商品列表页面
      gotoGoodsList(item) {
        uni.navigateTo({
          url:"/subpkg/goods_list/goods_list?cid="+ item.cat_id
        })
      },
      goSearch() {
        console.log('点击搜索...')
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scroll-view {
      width: 120px;

      .left-scroll-view-item {
        background-color: #f7f7f7;
        line-height: 60px;
        text-align: center;
        font-size: 12px;

        &.active {
          background-color: #fff;
          position: relative;

          // 激活选项伪元素
          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 40px;
            background-color: #c00000;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
          }
        }
      }

    }

    .cate-lv2-title {
      font-size: 12px;
      font-weight: bold;
      text-align: center;
      padding: 15px 0;
    }

    .cate-lv3-list {
      display: flex;
      flex-wrap: wrap;

      .cate-lv3-item {
        width: 33.33%;
        margin-bottom: 10px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        image {
          width: 60px;
          height: 60px;
        }

        text {
          font-size: 12px;
        }
      }
    }
  }
</style>
